package com.mobileapp.baliairport.src.main;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.mobileapp.baliairport.src.entity.fragment.FragmentArrayList;
import com.mobileapp.baliairport.src.listFlight.flightList;

@SuppressLint("NewApi")
public class MainTabActivity extends FragmentActivity {

	/* Tab identifiers */
	static String TAB_A = "SEARCH";
	static String TAB_B = "FLIGHT";
	static String TAB_C = "ACTIVITES";
	static String TAB_D = "HOME";
	TabHost mTabHost;

	public static String titleback = "";
	public static String titleTabA = "";
	public static String titleTabB = "";
	public static String titleTabC = "";

	fragmentFlightHeader fragmentFlightHeader;
	fragmentSearch fragmentSearch;
	fragmentShop fragmentShop;
	flightList AndroidXMLParsingActivity;
	
	public static ActionBar bar;
	private homeScreen homescreen;

	public static ArrayList<FragmentArrayList> fragmentdb = new ArrayList<FragmentArrayList>();
	public static FragmentArrayList frgArray;

	/*
	 * public static ArrayList<HashMap<String, Fragment>> fragments = new
	 * ArrayList<HashMap<String, Fragment>>(); public static
	 * ArrayList<HashMap<String, String>> tabName = new
	 * ArrayList<HashMap<String, String>>();
	 */
	// HashMap<String, Fragment> map;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main_tab);

		bar = getActionBar();

		homescreen = new homeScreen();
		fragmentFlightHeader = new fragmentFlightHeader();
		fragmentSearch = new fragmentSearch();
		fragmentShop = new fragmentShop();
		AndroidXMLParsingActivity = new flightList();

		this.addFragment(TAB_A, this.fragmentSearch);
		this.addFragment(TAB_B, this.fragmentFlightHeader);
		this.addFragment(TAB_C, this.fragmentShop);

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setOnTabChangedListener(listener);
		mTabHost.setup();

		initializeTab();
	}

	public void addFragment(String tabName, Fragment frg) {
		this.frgArray = new FragmentArrayList();
		this.frgArray.setTabName(tabName);
		this.frgArray.setFragment(frg);
		this.frgArray.setTitleBack("");
		this.frgArray.setBackFragment(null);
		this.fragmentdb.add(this.frgArray);

	}

	/*
	 * @Override public boolean onOptionsItemSelected(MenuItem menuItem) {
	 * startActivity(new Intent(CurrentActivity.this,PreviousActivity.class));
	 * return true;
	 * 
	 * 
	 * 
	 * return true; }
	 */

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			
			
			if(this.titleback.equals("")){
				
			}else{
				for(int i=0;i<this.fragmentdb.size();i++){
					if (MainTabActivity.titleback.equals(this.fragmentdb.get(i).getTitleBack())) {
						
						this.pushFragments(this.fragmentdb.get(i).getTabName(),
								this.fragmentdb.get(i).getBackFragment());
								
						
						Toast.makeText(getApplicationContext(),this.fragmentdb.get(i).getBackFragment().getClass().getName()+ "Back button clicked",
								Toast.LENGTH_SHORT).show();
						break;
					}
				}
			}
			
			

		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_tab, menu);
		return true;
	}

	/*
	 * Initialize the tabs and set views and identifiers for the tabs
	 */
	public void initializeTab() {

		TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
		// mTabHost.setCurrentTab(1);
		mTabHost.setCurrentTab(2);
		spec.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return findViewById(android.R.id.tabcontent);
			}
		});
		spec.setIndicator(createTabView(TAB_A, R.drawable.tab_icon1));
		mTabHost.addTab(spec);

		spec = mTabHost.newTabSpec(TAB_B);
		spec.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return findViewById(android.R.id.tabcontent);
			}
		});

		spec.setIndicator(createTabView(TAB_B, R.drawable.tab_icon2));
		mTabHost.addTab(spec);

		spec = mTabHost.newTabSpec(TAB_C);
		spec.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return findViewById(android.R.id.tabcontent);
			}
		});
		spec.setIndicator(createTabView(TAB_C, R.drawable.tab_icon2));
		mTabHost.addTab(spec);

		// pushFragments(TAB_C, homescreen);
		mTabHost.setCurrentTab(2);
	}

	/*
	 * TabChangeListener for changing the tab when one of the tabs is pressed
	 */
	TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
		public void onTabChanged(String tabId) {

			/* Set current tab.. */

			for (int a = 0; a < MainTabActivity.fragmentdb.size(); a++) {
				if (tabId
						.equals(MainTabActivity.fragmentdb.get(a).getTabName())) {
					Fragment tempFragment = MainTabActivity.fragmentdb.get(a).getFragment();
					
					pushFragments(tabId,tempFragment);
					
					break;
				}
				//

			}

			/*
			 * if (tabId.equals(TAB_A)) { if
			 * (tabId.equals(MainTabActivity.titleTabA)) {
			 * 
			 * } else { pushFragments(tabId, fragmentSearch);
			 * bar.setTitle("SEARCH"); }
			 * 
			 * // bar.setDisplayHomeAsUpEnabled(true); } else if
			 * (tabId.equals(TAB_B)) {
			 * 
			 * pushFragments(tabId, fragmentFlightHeader);
			 * bar.setTitle("FLIGHT HEADER"); MainTabActivity.titleTabB =
			 * "fragmentFlightHeader";
			 * 
			 * 
			 * } else if (tabId.equals(TAB_C)) { pushFragments(tabId,
			 * AndroidXMLParsingActivity); bar.setTitle("SHOP"); } else if
			 * (tabId.equals(TAB_D)) { pushFragments(tabId, homescreen); }
			 */
		}
	};

	/*
	 * adds the fragment to the FrameLayout
	 */
	public void pushFragments(String tag, Fragment fragment) {

		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction ft = manager.beginTransaction();

		ft.replace(android.R.id.tabcontent, fragment);
		ft.commit();

	}

	/*
	 * returns the tab view i.e. the tab icon and text
	 */
	private View createTabView(final String text, final int id) {
		View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
		// imageView.setImageDrawable(getResources().getDrawable(id));
		((TextView) view.findViewById(R.id.tab_text)).setText(text);
		return view;
	}
	
	public static void disableBackhome(){
		bar.setDisplayHomeAsUpEnabled(false);
	}

	public void changeFragment(Fragment kelas, String title, String titleTab,String Tbackhome,
			Fragment backhome) {

		// pushFragments("", AndroidXMLParsingActivity);
		bar.setTitle(title);
		bar.setDisplayHomeAsUpEnabled(true);
		String aku = kelas.getTag();
		
		for (int a = 0; a < this.fragmentdb.size(); a++) {
			if (this.fragmentdb.get(a).getTabName().equals(titleTab)) {
				this.frgArray = new FragmentArrayList();
				this.frgArray.setTabName(titleTab);
				this.frgArray.setTitleBack(Tbackhome);
				this.frgArray.setFragment(kelas);
				this.frgArray.setBackFragment(backhome);
				this.fragmentdb.add(a, this.frgArray);
				break;
			}
		}
		// this.titleback = "flightHeader";
		// this.titleTabB = "flightList";
		// Fragment t = kelas.getClass();
		pushFragments(titleTab, kelas);
	}

	// TODO Auto-generated method stub

}
/*
 * My last article was on basic fragment use. It showed how and when to use a
 * fragment. You can find the article at
 * http://www.c-sharpcorner.com/UploadFile/2fd686/fragments-in-android/
 * 
 * This article shows one of the major uses of fragments. You might have seen
 * many applications with bottom or top tab bars. In android we used to
 * implement it using TabActivity and ActivityGroup but now that these two have
 * been deprecated from the Android SDK, so here we will implement the same
 * functionality using fragments.
 * 
 * So lets start, create a new project and create the main class as
 * MainTabActivity.java and a layout file activity_main_tab.xml will be created
 * for you.
 * 
 * 1. Add two .png files to res/drawable folder which we will use for the icons
 * for tabs and name them tab_icon1.png and tab_icon2.png 2. Create a new file
 * tab_style.xml in res/drawable for the background of tabs. You can check out
 * this article :
 * http://www.c-sharpcorner.com/UploadFile/2fd686/android-buttons-background2/
 * to create an awesome background but for now you can use this code: 3. Create
 * tabs_icon.xml in res/layout, this is view for the tabs. You can use this
 * code: 4. Create fragmentFlightHeader.xml and fragmentSearch.xml in res/layout
 * folder. 5. Open activity_main_tab.xml from res/layout folder, the root
 * element is TabHost which contains the FrameLayout for the fragment and
 * TabWidget to hold the tabs and paste this code. 6. Now create 2 java files
 * fragmentFlightHeader.Java and fragmentSearch.java, they have the minimum code
 * for a Fragment. 7. Open MainTabActivity.java. I have added comments to the
 * code for the explanation. Paste this code and run the app.
 */

